package com.example.myapplication;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.View;

import java.util.Calendar;
import java.util.TimeZone;

public class AnalogClockView extends View {
    private Paint paint;
    private Rect rect;
    private int height, width = 0;
    private int padding = 0;
    private int fontSize = 0;
    private int numeralSpacing = 0;
    private int handTruncation, hourHandTruncation = 0;
    private int radius = 0;
    private boolean isInit;
    private TimeZone beijingTimeZone = TimeZone.getTimeZone("Asia/Shanghai");

    public AnalogClockView(Context context) {
        super(context);
    }

    public AnalogClockView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public AnalogClockView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    private void initClock() {
        height = getHeight();
        width = getWidth();
        padding = numeralSpacing + 50;
        fontSize = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, 13,
                getResources().getDisplayMetrics());
        int min = Math.min(height, width);
        radius = min / 2 - padding;
        handTruncation = min / 20;
        hourHandTruncation = min / 7;
        paint = new Paint();
        isInit = true;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        if (!isInit) {
            initClock();
        }

        canvas.drawColor(Color.TRANSPARENT);
        drawCircle(canvas);
        drawCenter(canvas);
        drawNumeral(canvas);
        drawHands(canvas);

        postInvalidateDelayed(500);
        super.onDraw(canvas);
    }

    private void drawCircle(Canvas canvas) {
        paint.reset();
        paint.setColor(getResources().getColor(android.R.color.black));
        paint.setStrokeWidth(5);
        paint.setStyle(Paint.Style.STROKE);
        paint.setAntiAlias(true);
        canvas.drawCircle(width / 2, height / 2, radius + padding - 10, paint);
    }

    private void drawCenter(Canvas canvas) {
        paint.setStyle(Paint.Style.FILL);
        canvas.drawCircle(width / 2, height / 2, 12, paint);
    }

    private void drawNumeral(Canvas canvas) {
        paint.setTextSize(fontSize);
        paint.setStyle(Paint.Style.FILL);

        double angle = Math.PI / 6;
        for (int i = 1; i < 13; i++) {
            String tmp = String.valueOf(i);
            double angleI = i * angle;
            int x = (int) (width / 2 + Math.cos(angleI) * radius - paint.measureText(tmp) / 2);
            int y = (int) (height / 2 + Math.sin(angleI) * radius + fontSize / 3);
            canvas.drawText(tmp, x, y, paint);
        }
    }

    private void drawHands(Canvas canvas) {
        Calendar calendar = Calendar.getInstance(beijingTimeZone);
        float hour = calendar.get(Calendar.HOUR_OF_DAY);
        hour = hour > 12 ? hour - 12 : hour;
        float minute = calendar.get(Calendar.MINUTE);
        float second = calendar.get(Calendar.SECOND);
        float millisecond = calendar.get(Calendar.MILLISECOND);

        // 计算精确的角度（包括秒和毫秒的分数）
        float hourAngle = (hour + minute / 60f) * 30f;
        float minuteAngle = (minute + second / 60f) * 6f;
        float secondAngle = (second + millisecond / 1000f) * 6f;

        drawHand(canvas, hourAngle, radius - hourHandTruncation, 8, Color.BLACK);
        drawHand(canvas, minuteAngle, radius - handTruncation, 5, Color.BLACK);
        drawHand(canvas, secondAngle, radius - handTruncation + 10, 2, Color.RED);
    }

    private void drawHand(Canvas canvas, float angle, int handRadius, int strokeWidth, int color) {
        float angleRad = (float) (angle * Math.PI / 180);
        int x = (int) (width / 2 + Math.cos(angleRad - Math.PI / 2) * handRadius);
        int y = (int) (height / 2 + Math.sin(angleRad - Math.PI / 2) * handRadius);
        paint.setStrokeWidth(strokeWidth);
        paint.setColor(color);
        paint.setStrokeCap(Paint.Cap.ROUND);
        canvas.drawLine(width / 2, height / 2, x, y, paint);
    }
}
